﻿/* MVT Charts for Silverlight - Version 1
 * 2008 by Mario Meir-Huber
 * mailto: i-mameir@microsoft.com
 *         mario@meirhuber.de
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ChartLibrary.Data;
using System.Globalization;
using ChartLibrary.Base;

/*
 * ChartLibrary.Charts is the Namespace for all charts.
 */
namespace ChartLibrary.Charts
{
    /// <summary>
    /// Displays the easiest way of a linechart
    /// </summary>
    public class LineChart : AxisCharts
    {
        private bool initialized = false;

        /// <summary>
        /// displays a single line chart
        /// </summary>
        public LineChart()
        {
            this.SizeChanged += new SizeChangedEventHandler(LineChart_SizeChanged);
            this.DrawAreaChanged += new StateChangedEvent(SLineChart_LayoutUpdated);
            this.Loaded += new RoutedEventHandler(SLineChart_Loaded);
        }

        /// <summary>
        /// updates a linechart 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SLineChart_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateChart();
        }

        /// <summary>
        /// updates a linechart when the layout was updated
        /// </summary>
        void SLineChart_LayoutUpdated()
        {
            UpdateChart();
        }

        /// <summary>
        /// updates a linechart when the size was changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void LineChart_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            UpdateChart();
        }

        /// <summary>
        /// updates a chart
        /// </summary>
        private void UpdateChart()
        {
            double max = Data.Max;
            double min = Data.Min;
            Line tmpelem;
            double singleleft;

            double singletop = Area.Height / (max - min);

            double lastx = 0.0, lasty = 0.0;
            bool firstpoint;

            int j = -1;

            Storyboard sttemp = null;
            Storyboard stlineanim;

            //iterates over all datasets
            foreach (DataSource ds in Data)
            {
                firstpoint = true;
                singleleft = (Area.Width - Area.StartX) / (ds.Length - 1);
                j++;

                if (!initialized) sttemp = AddAnimation(j);

                for (int i = 0; i < ds.Length; i++)
                {
                    if (firstpoint)
                    {
                        lastx = Area.StartX + singleleft * i;
                        lasty = Area.StartY + Area.Height - (ds[i].Value - min) * singletop;

                        firstpoint = false;
                    }
                    else
                    {
                        tmpelem = LayoutRoot.FindName("_ptmarker_" + i.ToString() + "_" + j.ToString()) as Line;

                        if (null == tmpelem)
                        {
                            tmpelem = new Line();
                            tmpelem.SetValue(Canvas.NameProperty, "_ptmarker_" + i.ToString() + "_" + j.ToString());

                            tmpelem.Stroke = ds.Fill;
                            tmpelem.StrokeThickness = 1;
                            LayoutRoot.Children.Add(tmpelem);
                        }

                        if (null != tmpelem)
                        {
                            stlineanim = MakeStoryBoard(tmpelem, sttemp, i, tmpelem.X2, tmpelem.Y2, tmpelem);

                            //LayoutRoot.Resources.Add(stlineanim);
                            stlineanim.Completed += new EventHandler(stlineanim_Completed);
                            stlineanim.Begin();
                        }

                        tmpelem.X1 = lastx;
                        tmpelem.Y1 = lasty;

                        tmpelem.X2 = Area.StartX + singleleft * i;
                        tmpelem.Y2 = Area.StartY + Area.Height - (ds[i].Value - min) * singletop;

                        lastx = tmpelem.X2;
                        lasty = tmpelem.Y2;
                    }
                }

                try
                {
                    //this.Resources.Add(sttemp);
                    sttemp.Completed += new EventHandler(sttemp_Completed);
                    sttemp.Begin();
                }
                catch
                {
                    Console.WriteLine("Error Occured in LineChart.cs");
                }
            }

            initialized = true;
        }

        /// <summary>
        /// removes animations when they have completed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void stlineanim_Completed(object sender, EventArgs e)
        {
            Storyboard st = (Storyboard)sender;
            //LayoutRoot.Resources.Remove(st);
        }

        /// <summary>
        /// removes a storyboard when it is completed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void sttemp_Completed(object sender, EventArgs e)
        {
            Storyboard st = (Storyboard)sender;
            //this.Resources.Remove(st);
        }

        /// <summary>
        /// adds an animation
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private Storyboard AddAnimation(int id)
        {
            Storyboard retval = new Storyboard();
            retval.SetValue(Canvas.NameProperty, "_story" + id);
            return retval;
        }

        /// <summary>
        /// this generates a new storyboard for a single point
        /// </summary>
        /// <param name="tmpelem">Element that should be animated</param>
        /// <param name="sttemp">Storyboard to use</param>
        /// <param name="AsynchID">Indicates if it should be used Asynchronous</param>
        /// <param name="xpos">Animate to an X Position</param>
        /// <param name="ypos">Animate to an Y Position</param>
        /// <returns></returns>
        private Storyboard MakeStoryBoard(UIElement tmpelem, Storyboard sttemp, int AsynchID, double xpos, double ypos)
        {
            string initstory = "";

            double calculated_length = 2.0;

            if (Data.Count > 0)
            {
                calculated_length /= Data[0].Length;
            }

            switch (Animation)
            {
                case ChartLibrary.Base.Animation.FxOpacity:
                    {
                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.Opacity)\" " +
                        "BeginTime=\"00:00:00\"><SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"0\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5000000\" Value=\"1\"/></DoubleAnimationUsingKeyFrames></Storyboard>";
                        break;
                    }
                case ChartLibrary.Base.Animation.FxOpacityAsynchrounous:
                    {
                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.Opacity)\" " +
                        "BeginTime=\"00:00:00\"><SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"0\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.2 * AsynchID * calculated_length, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"1\"/></DoubleAnimationUsingKeyFrames></Storyboard>";
                        break;
                    }
                case ChartLibrary.Base.Animation.FxCenterPoint:
                    {
                        double new_xpos = Math.Round(ActualWidth / 2 - xpos, 2);
                        double new_ypos = Math.Round(ActualHeight / 2 - ypos, 2);

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform.X)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + new_xpos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"0\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform.Y)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + new_ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"0\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +

                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxCenterPointAsynchronous:
                    {
                        double new_xpos = Math.Round(ActualWidth / 2 - xpos, 2);
                        double new_ypos = Math.Round(ActualHeight / 2 - ypos, 2);

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform.X)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + new_xpos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * AsynchID * calculated_length + 0.2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"0\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform.Y)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + new_ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * AsynchID * calculated_length + 0.2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"0\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +

                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxDropDownAsynchronous:
                    {
                        double new_ypos = ypos * -1;

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform.Y)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + new_ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * AsynchID * calculated_length + 0.1, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"10\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * AsynchID * calculated_length + 0.2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"0\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +

                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxDropDown:
                    {
                        double new_ypos = ypos * -1;

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform.Y)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + new_ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (0.4).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"10\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (0.5).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"0\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +

                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxRiseUpAsynchronous:
                    {
                        double new_ypos = ActualHeight - ypos;

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform.Y)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + new_ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * AsynchID * calculated_length + 0.1, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"-10\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * AsynchID * calculated_length + 0.2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"0\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +

                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxRiseUp:
                    {
                        double new_ypos = ActualHeight - ypos;

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tmpelem.GetValue(Canvas.NameProperty).ToString() + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform.Y)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + new_ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (0.4).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"-10\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (0.5).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"0\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +

                        "</Storyboard>";

                        break;
                    }
                default:
                    {
                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\" />";
                        break;
                    }
            }

            sttemp = (Storyboard)System.Windows.Markup.XamlReader.Load(initstory);
            return sttemp;
        }

        /// <summary>
        /// Generates a Storyboard
        /// </summary>
        /// <param name="tmpelem">Element to be used</param>
        /// <param name="sttemp">Storyboard</param>
        /// <param name="AsynchID">indicates the asynch ID to use</param>
        /// <param name="xpos">Y Position to animate to</param>
        /// <param name="ypos">X Position to animate to</param>
        /// <param name="tmpline">Line that should be used</param>
        /// <returns></returns>
        private Storyboard MakeStoryBoard(UIElement tmpelem, Storyboard sttemp, int AsynchID, double xpos, double ypos, UIElement tmpline)
        {
            string initstory = "";
            string tline = (string)tmpline.GetValue(Canvas.NameProperty);

            double x1pos = (double)tmpline.GetValue(Line.X1Property);
            double y1pos = (double)tmpline.GetValue(Line.Y1Property);

            double calculated_length = 2.0;

            if (Data.Count > 0)
            {
                calculated_length /= Data[0].Length;
            }

            switch (Animation)
            {
                case ChartLibrary.Base.Animation.FxOpacity:
                    {
                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Opacity)\" " +
                        "BeginTime=\"00:00:00\"><SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"0\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5000000\" Value=\"1\"/></DoubleAnimationUsingKeyFrames></Storyboard>";
                        break;
                    }
                case ChartLibrary.Base.Animation.FxOpacityAsynchrounous:
                    {
                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Opacity)\" " +
                        "BeginTime=\"00:00:00\"><SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"0\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.2 * AsynchID * calculated_length, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"1\"/></DoubleAnimationUsingKeyFrames></Storyboard>";
                        break;
                    }
                case ChartLibrary.Base.Animation.FxCenterPoint:
                    {
                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y2)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + (ActualHeight / 2).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"" + ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y1)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + (ActualHeight / 2).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"" + y1pos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.X2)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + (ActualWidth / 2).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"" + xpos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.X1)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + (ActualHeight / 2).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"" + x1pos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxCenterPointAsynchronous:
                    {
                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y2)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + (Math.Round(ActualHeight / 2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:0" + (Math.Round(0.1 * (AsynchID - 1) * calculated_length + 0.2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y1)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + (Math.Round(ActualHeight / 2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:0" + (Math.Round(0.1 * AsynchID * calculated_length + 0.2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + y1pos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.X2)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + (Math.Round(ActualWidth / 2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:0" + (Math.Round(0.1 * (AsynchID - 1) * calculated_length + 0.2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + xpos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.X1)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + (Math.Round(ActualHeight / 2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:0" + (Math.Round(0.1 * AsynchID * calculated_length + 0.2, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + x1pos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxDropDownAsynchronous:
                    {
                        double new_ypos = ActualHeight - ypos;

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y2)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"0\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * AsynchID * calculated_length + 0.1, 2)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + (ypos + 10).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * AsynchID * calculated_length + 0.2, 2)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y1)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"0\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * (AsynchID - 1) * calculated_length + 0.1, 2)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + (y1pos + 10).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round(0.1 * (AsynchID - 1) * calculated_length + 0.2, 2)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + y1pos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxDropDown:
                    {
                        double new_ypos = ActualHeight - ypos;

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y2)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"0\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.4\" Value=\"" + (ypos + 10).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"" + ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y1)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"0\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.4\" Value=\"" + (y1pos + 10).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"" + y1pos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxRiseUpAsynchronous:
                    {
                        double new_ypos = ActualHeight - ypos;

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y2)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + ActualHeight.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round((0.1 * AsynchID + 0.1) * calculated_length, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + (ypos - 10).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round((0.1 * AsynchID + 0.2) * calculated_length, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y1)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + ActualHeight.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round((0.1 * (AsynchID - 1) + 0.1) * calculated_length, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + (y1pos - 10).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:" + (Math.Round((0.1 * (AsynchID - 1) + 0.2) * calculated_length, 3)).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\" Value=\"" + y1pos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "</Storyboard>";

                        break;
                    }
                case ChartLibrary.Base.Animation.FxRiseUp:
                    {
                        double new_ypos = ActualHeight - ypos;

                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\">" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y2)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + ActualHeight.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.4\" Value=\"" + (ypos - 10).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"" + ypos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "<DoubleAnimationUsingKeyFrames Storyboard.TargetName=\"" + tline + "\" Storyboard.TargetProperty=\"(UIElement.Y1)\" " +
                        "BeginTime=\"00:00:00\">" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00\" Value=\"" + ActualHeight.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.4\" Value=\"" + (y1pos - 10).ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "<SplineDoubleKeyFrame KeyTime=\"00:00:00.5\" Value=\"" + y1pos.ToString(new NumberFormatInfo() { CurrencyDecimalSeparator = "." }) + "\"/>" +
                        "</DoubleAnimationUsingKeyFrames>" +
                        "</Storyboard>";

                        break;
                    }
                default:
                    {
                        initstory = "<Storyboard xmlns=\"http://schemas.microsoft.com/client/2007\" />";
                        break;
                    }
            }

            try
            {
                sttemp = (Storyboard)System.Windows.Markup.XamlReader.Load(initstory);
            }
            catch 
            {
                sttemp = new Storyboard();
            }

            return sttemp;
        }
    }
}